package comxin.demo3;

import org.junit.Test;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class Fielddemo {
    //返回数组
    @Test
    public void method1() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("comxin.Student");
        Field[] fields = aClass.getDeclaredFields();
        for (Field field:fields){
            System.out.println(field);
        }
    }
    //返回单个
    @Test
    public void method2() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> aClass = Class.forName("comxin.Student");
        Field aaa = aClass.getField("age");
        Constructor<?> constructor = aClass.getConstructor();
        Object o = constructor.newInstance();
        aaa.set(o,18);
        System.out.println(o);


    }
}
